using System;
using System.ComponentModel;
using System.Windows.Forms;
using Toolbox.Plugin.File;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.Collections.Generic;

namespace Toolbox.Plugin.Controls
{
    [ToolboxItem(typeof(IPluginSource))]
    public partial class LoadFilePluginMenuItem : PluginMenuItem
    {
        [Browsable(false)]
        public string FileName { get; set; }

        public LoadFilePluginMenuItem()
            : base()
        {
            InitializeComponent();
        }

        public override void OnImportsSatisfied()
        {
            this.BuildMenus<IPluginImporter>();
        }

        protected override void OnPluginItemClicked(object sender, EventArgs e)
        {
            if (sender is PluginToolStripItem)
            {
                this.LoadDialog.Tag = sender;
                this.LoadDialog.Title = "Loading " + (sender as PluginToolStripItem).PluginItem.Description;
                this.LoadDialog.Filter = "All files (*.*)|*.*";
                {
                    foreach (PluginFileAttribute attr in (sender as PluginToolStripItem).PluginItem.GetAttributes<PluginFileAttribute>())
                    {
                        this.LoadDialog.Filter = attr.FileType + " (*." + attr.Extension + ")|*." + attr.Extension + "|" + this.LoadDialog.Filter;
                    }
                }

                this.LoadDialog.ShowDialog();
            }
        }

        private void LoadDialog_FileOk(object sender, CancelEventArgs e)
        {
            if (!e.Cancel)
            {
                PluginToolStripItem tsi = this.LoadDialog.Tag as PluginToolStripItem;

                if (tsi is PluginToolStripItem && tsi.PluginItem.Plugin is IPluginImporter)
                {
                    (tsi.PluginItem.Plugin as IPluginImporter).FileName = this.LoadDialog.FileName;
                    base.OnPluginItemClicked(tsi, EventArgs.Empty);
                }
            }
        }

        //protected override void DoAction(IPluginAction p)
        //{
        //    if (this.FileLoading != null)
        //    {
        //        this.FileLoading(this, new ProgressChangedEventArgs(0, null));
        //    }

        //    base.DoAction(p);
        //}

        //public event ProgressChangedEventHandler FileLoading;
        //public event RunWorkerCompletedEventHandler FileLoaded;

        //protected override object OnDoWork(IPluginAction p)
        //{
        //    if (p is IPluginImporter)
        //    {
        //        return (p as IPluginImporter).Load();
        //    }
        //    else
        //    {
        //        return base.OnDoWork(p);
        //    }
        //}

        //protected override void OnProgressChanged(ProgressChangedEventArgs e)
        //{
        //    base.OnProgressChanged(e);

        //    if (this.FileLoading != null)
        //    {
        //        this.FileLoading(this, e);
        //    }
        //}

        //protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        //{
        //    base.OnRunWorkerCompleted(e);

        //    this.OnFileImport(e.Result);

        //    if (this.FileLoaded != null)
        //    {
        //        this.FileLoaded(this, e);
        //    }
        //}

        protected virtual void OnFileImport(object data)
        {
        }
    }

}
